from utils.ext import inner_db
from marshmallow import Schema, fields, ValidationError, validate, validates_schema

class ImagesAddSchema(Schema):
    image_name = fields.String(required=True)
    image_registry = fields.String(required=True)
    framework = fields.String(required=True)
    language = fields.String(required=True)
    provider = fields.String()

class ImagesUpdateSchema(Schema):
    image_name = fields.String(required=True)
    image_registry = fields.String(required=True)
    provdier = fields.String()
    framework = fields.String()
    language = fields.String()

class ImagesDeleteSchema(Schema):
    image_registry = fields.String(required=True)
    framework = fields.String(default="")
    language = fields.String(default="")

class Framework(inner_db.Model):
    __bind_key__ = 'cnbita'
    __tablename__ = 'pai_framework'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True, autoincrement=True, nullable=False)
    name = inner_db.Column(inner_db.String(100), nullable=False)
    del_flag = inner_db.Column(inner_db.SmallInteger, nullable=False, default=0)
    create_time = inner_db.Column(inner_db.BigInteger)
    update_time = inner_db.Column(inner_db.BigInteger)

    def get_data_info(self):
        return dict(id = self.id, name = self.name, del_flag = self.del_flag,
                    create_time = self.create_time, update_time = self.update_time)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())


class Image(inner_db.Model):
    __bind_key__ = 'cnbita'
    __tablename__ = 'pai_images'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True, autoincrement=True, nullable=False)
    number = inner_db.Column(inner_db.String(100), nullable=False)
    name = inner_db.Column(inner_db.String(255), nullable=False)
    path = inner_db.Column(inner_db.String(255))
    domain = inner_db.Column(inner_db.String(255), nullable=False)
    provider = inner_db.Column(inner_db.String(255))
    type = inner_db.Column(inner_db.SmallInteger, nullable=False, default=0)
    del_flag = inner_db.Column(inner_db.SmallInteger, nullable=False, default=0)
    create_time = inner_db.Column(inner_db.BigInteger)
    update_time = inner_db.Column(inner_db.BigInteger)

    def get_data_info(self):
        return dict(id = self.id,
                    name = self.name,
                    path = "{}{}".format(self.domain, self.path),
                    provider = self.provider,
                    type = self.type,
                    create_time = self.create_time,
                    update_time = self.update_time)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())

class ImageGetScheme(Schema):
    per_page_items = fields.Integer(missing=10)
    page_index = fields.Integer(missing=1)
    sort = fields.String(missing="default")

class RelationMap(inner_db.Model):
    __bind_key__ = 'cnbita'
    __tablename__ = 'pai_images_mapping_relation'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True, autoincrement=True, nullable=False)
    language_id = inner_db.Column(inner_db.BigInteger, nullable=False)
    framework_id = inner_db.Column(inner_db.BigInteger, nullable=False)
    image_id = inner_db.Column(inner_db.BigInteger, nullable=False)
    del_flag = inner_db.Column(inner_db.SmallInteger, nullable=False, default=0)
    create_time = inner_db.Column(inner_db.BigInteger)
    update_time = inner_db.Column(inner_db.BigInteger)

    def get_data_info(self):
        return dict(id = self.id, language_id = self.language_id, framework_id = self.framework_id,
                    image_id = self.image_id, del_flag = self.del_flag,
                    create_time = self.create_time, update_time = self.update_time)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())

class Language(inner_db.Model):
    __bind_key__ = 'cnbita'
    __tablename__ = 'pai_language'

    id = inner_db.Column(inner_db.BigInteger, primary_key=True, autoincrement=True, nullable=False)
    name = inner_db.Column(inner_db.String(100), nullable=False)
    del_flag = inner_db.Column(inner_db.SmallInteger, nullable=False, default=0)
    create_time = inner_db.Column(inner_db.BigInteger)
    update_time = inner_db.Column(inner_db.BigInteger)

    def get_data_info(self):
        return dict(id = self.id, name = self.name, del_flag = self.del_flag,
                    create_time = self.create_time, update_time = self.update_time)

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())
